<template>
	<view>
		<view class="content">
			<view class="row b-b">
				<text class="tit">联系人</text>
				<input class="input" type="text" placeholder="收货人姓名" placeholder-class="placeholder" v-model="name"/>
			</view>
			<view class="row b-b">
				<text class="tit">手机号</text>
				<input class="input" type="number" placeholder="收货人手机号码" placeholder-class="placeholder" v-model="phone"/>
			</view>
			<view class="row b-b">
				<text class="tit">选择地址</text>
				<u-picker v-model="show" mode="region" @confirm="confirm"></u-picker>
				<view @click="show = true">{{add}}</view>
			</view>
		</view>
		<view class="row b-b">
			<text class="tit">详细地址</text>
			<input class="input" type="text" placeholder="请输入详细地址" placeholder-class="placeholder" v-model="detail"/>
		</view>
		<view class="row default-row">
			<text class="tit">设为默认</text>
			<switch color="#fc9f56" :checked="isDefaultAddress" @change="switch1Change"/>
		</view>
		<button class="add-btn" @click="addRess">提交</button>
	</view>
	
</template>

<script>
	import {address} from'../../api/login.js'
	export default {
			data() {
				return {
					show: false,
					add:"选择地址",
					isDefaultAddress:false,//默认地址
					name:"",
					phone:"",
					provinceCode:'',//省编码
					provinceName:"",//省名称
					cityCode:"",//市编码
					cityName:"",//市名称
					regionCode:"",//区编码
					regionName:'',//区名称
					detail:"",//详细地址
					addressId:null//地址id
				}
			},
			onLoad(e){
				if(e.obj){
					console.log(JSON.parse(e.obj))
					let data=JSON.parse(e.obj)
					this.name=data.name
					this.phone=data.phone
					this.isDefaultAddress=data.isDefaultAddress
					this.add=data.provinceName+data.cityName+data.regionName
					this.detail=data.detail
					this.addressId=data.addressId
					this.provinceCode=data.provinceId
					this.cityCode=data.cityId
					this.regionCode=data.regionId
					this.provinceName=data.provinceName
					this.cityName=data.cityName
					this.regionName=data.regionName
				}
			},
			methods:{
				// 选择地址
				confirm(e){
					console.log(e)
					this.add=e.province.label+e.city.label+e.area.label
					this.provinceCode=e.province.value+"0000"
					this.provinceName=e.province.label
					this.cityCode=e.city.value+"00"
					this.cityName=e.city.label
					this.regionCode=e.area.value
					this.regionName=e.area.label
				},
				// 选择默认
				switch1Change(e){
					console.log(e)
					this.isDefaultAddress=e.detail.value
				},
				async addRess(){
					if(!this.$u.test.chinese(this.name)){
						this.msg("姓名格式不正确","none")
						return
					}
					if(!this.$u.test.mobile(this.phone)){
						this.msg("手机号格式不正确","none")
						return
					}
					if(this.add=="选择地址"){
						this.msg("省市区未选择","none")
						return
					}
					if(this.detail.trim()==""){
						this.msg("详细地址未填写","none")
						return
					}
					let data={
						provinceCode:this.provinceCode,
						provinceName:this.provinceName,
						cityCode:this.cityCode,
						cityName:this.cityName,
						regionCode:this.regionCode,
						regionName:this.regionName,
						detail:this.detail,
						phone:this.phone,
						name:this.name,
						isDefaultAddress:this.isDefaultAddress
					}
					if(this.addressId!=null){
						data.addressId=this.addressId
					}
					console.log("data",data)
					let res=await this.post(address,data)
					console.log(res)
					this.msg("保存成功")
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},1500)
				}
			}
		}
</script>

<style lang="scss">
	page {
		background: #f6f6f6;
		padding-top: 16upx;
	}

	.row {
		display: flex;
		align-items: center;
		position: relative;
		padding: 0 30upx;
		height: 110upx;
		background: #fff;

		.tit {
			flex-shrink: 0;
			width: 140upx;
			font-size: 30upx;
			// color: $font-color-dark;
		}

		.input {
			flex: 1;
			font-size: 30upx;
			// color: $font-color-dark;
		}

		.iconfont {
			font-size: 36upx;
			// color: $font-color-light;
		}
	}

	.default-row {
		margin-top: 16upx;

		.tit {
			flex: 1;
		}

		switch {
			transform: translateX(16upx) scale(.9);
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		// font-size: $font-lg;
		color: #fff;
		background-color: #fc9f56;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
